cURL works but PHP cURL fails to internet [migrated]

Posted by wrk2bike on Pro Webmasters See other posts from Pro Webmasters or by wrk2bike
Published on 2014-08-22T17:03:43Z Indexed on 2014/08/22 22:35 UTC
Read the original article Hit count: 526

Filed under:
|

Trying to diagnose an issue using PHP to cURL to an Internet location on a RedHat Linux server.

cURL is installed and working, and:

<?php var_dump(curl_version()); ?>

shows all the correct information in the output. The issue is I can use PHP to cURL to localhost on the box itself, but not the Internet (see below).

Normally I'd suspect the firewall, but I can cURL from the command line to the Internet without a problem. The box can also update it's own software packages, etc.

What am I missing? My test is:

<?php 
    function http_head_curl($url,$timeout=30)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_NOBODY, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($ch);
        if ($res === false) {
            throw new RuntimeException("cURL exception: ".curl_errno($ch).": ".curl_error($ch));
        }
        return trim($res);
    }

    // Succeeds, displaying headers
    echo(http_head_curl('localhost'));

    // Fails:
    echo(http_head_curl('www.google.com'));
?>

© Pro Webmasters or respective owner

Related posts about php

Related posts about url